package com.lagou.buffer;

import java.nio.ByteBuffer;

/**
 * @description:
 * @author: yx
 * @date: 2021/11/5 9:33
 */
public class CreateBufferDemo {

    public static void main(String[] args) {
        //1. allocate创建一个指定长度的缓冲区
        ByteBuffer allocate = ByteBuffer.allocate(5);
        for (int i = 0; i < 5; i++) {
            System.out.println(allocate.get());
        }
        //获取指定长度外的数据会报错
        //System.out.println(allocate.get());
        System.out.println("===============================");
        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(5);
        for (int i = 0; i < 5; i++) {
            System.out.println(allocateDirect.get());
        }
        //allocateDirect.get();
        System.out.println("===============================");
        //2. wrap创建一个有内容的的缓存区
        String abc = "abc";
        ByteBuffer wrap = ByteBuffer.wrap(abc.getBytes());
        for (int i = 0; i < abc.length(); i++) {
            System.out.println(wrap.get());
        }
        System.out.println("===============================");
        ByteBuffer wrap1 = ByteBuffer.wrap(abc.getBytes(), 1, 2);
        for (int i = 0; i < 1; i++) {
            System.out.println(wrap1.get());
        }
    }
}
